package com.light.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Map;

import com.light.common.enums.ExceptionEnum;
import com.light.gulimall.ware.vo.FareVo;
import com.light.gulimall.ware.vo.StockLockResultVO;
import com.light.gulimall.ware.vo.StockLockVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.light.gulimall.ware.entity.WareInfoEntity;
import com.light.gulimall.ware.service.WareInfoService;
import com.light.common.utils.PageUtils;
import com.light.common.utils.R;



/**
 * 仓库信息
 *
 * @author minmin
 * @email 980680541@gmail.com
 * @date 2024-07-16 21:32:14
 */
@RestController
@RequestMapping("ware/wareinfo")
public class WareInfoController {
    @Autowired
    private WareInfoService wareInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = wareInfoService.queryPage(params);
        PageUtils page = wareInfoService.queryByCondition(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询运费
     * @param addrId
     * @return
     */
    @GetMapping("/fare")
    public FareVo getFare(@RequestParam("addrId") Long addrId) {
        return wareInfoService.getFare(addrId);
    }

    /**
     * 给product模块远程调用，锁定库存
     * @param vo
     * @return
     */
    @PostMapping("/order/stockNum")
    public R stockLock(@RequestBody StockLockVO vo) {
        try {
            wareInfoService.stockNum(vo);
            return R.ok();
        } catch (Exception e) {
            return R.error(ExceptionEnum.NO_STOCK_EXCEPTION.getCode(),ExceptionEnum.NO_STOCK_EXCEPTION.getValue());
        }

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WareInfoEntity wareInfo = wareInfoService.getById(id);

        return R.ok().put("wareInfo", wareInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareInfoEntity wareInfo){
		wareInfoService.save(wareInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareInfoEntity wareInfo){
		wareInfoService.updateById(wareInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		wareInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
